﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SQLite;
using WMS.DAL.DBUtility;
using System.Data;
using WMS.Entity;

namespace WMS.DAL.SQLite
{
    public class GoodsLogDB : WMS.IDAL.IGoodsLog
    {
        #region IGoodsLog 成员

        private static readonly string sqlInsert = "insert into goodslog (goodscode,price,quantity,type,createdate)"
                                                    + " values(@goodscode,@price,@quantity,@type,@createdate)";
        private static readonly string sqlSelectRecords = "select * from goodslog where goodscode={0}";

        public int Add(WMS.Entity.GoodsLogEntity entity)
        {
            SQLiteParameter[] para = new SQLiteParameter[] 
            {
                new SQLiteParameter("@goodscode",DbType.Int32),
                new SQLiteParameter("@price",DbType.Decimal),
                new SQLiteParameter("@quantity",DbType.Int32),
                new SQLiteParameter("@type",DbType.Int32),
                new SQLiteParameter("@createdate",DbType.DateTime),
                new SQLiteParameter("@autoid",DbType.Int32)
            };

            para[0].Value = entity.GoodsCode;
            para[1].Value = entity.Price;
            para[2].Value = entity.Quantity;
            para[3].Value = entity.Type;
            para[4].Value = entity.CreateDate;
            para[5].Value = entity.AutoID;

            return SQLiteHelper.ExecuteNonQuery(sqlInsert,para);
        }

        public IList<WMS.Entity.GoodsLogEntity> GetGoodsRecords(int goodsID)
        {
            IList<GoodsLogEntity> list = new List<GoodsLogEntity>();

            using (SQLiteDataReader reader = SQLiteHelper.ExecuteReader(SQLiteHelper.ConnectionString, CommandType.Text, string.Format(sqlSelectRecords,goodsID), null))
            {
                while (reader.Read())
                {
                    GoodsLogEntity entity = new GoodsLogEntity();
                    entity.AutoID = int.Parse(reader["autoid"].ToString());
                    entity.GoodsCode = reader["goodscode"].ToString();
                    entity.Price = decimal.Parse(reader["price"].ToString());
                    entity.Quantity = int.Parse(reader["quantity"].ToString());
                    entity.Type = int.Parse(reader["type"].ToString());
                    entity.CreateDate = DateTime.Parse(reader["createdate"].ToString());

                    list.Add(entity);
                }

                if (reader.IsClosed == false)
                    reader.Close();
            }

            return list;
        }

        public int Update(int autoID)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
